import axios from 'axios'
import { ElMessage } from 'element-plus'

import router from '../router/index.js'


const http_request = axios.create({
    // baseURL: 'http://39.101.141.162:8000',
	baseURL: 'http://127.0.0.1:8000',
	validateStatus: function(status){
		return true
	}
})


http_request.interceptors.request.use(function(config){
	if (config.url != '/user/login/' && config.url != '/user/register/'){
		config.headers['Authorization'] = 'Bearer ' + JSON.parse(window.localStorage.getItem('token'))
	}
	return config
})

http_request.interceptors.response.use(function(res){
	if (res.status === 401){
		router.push({name: 'login'})
	}
	if(res.status === 400){
		for(let error_obj in res.data.result){
			console.log(res.data.result[error_obj], 999)
			ElMessage({
			  type: 'error',
			  duration: 5000,
			  showClose: true, 
			  message: res.data.result[error_obj][0],
			})
		}
	}
	return res
})


export default {
	// 环境管理接口
	getEnvs(params){
		return http_request.get('/env/env/', {params: params})
	},
	getEnv(id){
		return http_request.get(`/env/env/${id}`)
	},
	updateEnv(id, params){
		return http_request.put(`/env/env/${id}/`, params)
	},
	createEnv(params){
		return http_request.post('/env/env/', params)
	},
	deleteEnv(id){
		return http_request.delete(`/env/env/${id}/`)
	},
	// 变量管理接口
	getPars(params){
		return http_request.get('/env/params/', {params: params})
	},
	getPar(id){
		return http_request.get(`/env/params/${id}`)
	},
	updatePar(id, params){
		return http_request.put(`/env/params/${id}/`, params)
	},
	createPar(params){
		return http_request.post('/env/params/', params)
	},
	deletePar(id){
		return http_request.delete(`/env/params/${id}/`)
	},
	// 服务管理接口
	getServices(params){
		return http_request.get('/env/service/', {params: params})
	},
	getService(id){
		return http_request.get(`/env/service/${id}`)
	},
	updateService(id, params){
		return http_request.put(`/env/service/${id}/`, params)
	},
	createService(params){
		return http_request.post('/env/service/', params)
	},
	deleteService(id){
		return http_request.delete(`/env/service/${id}/`)
	},
	// 环境服务管理接口
	getEnvServices(params){
		return http_request.get('/env/env_service/', {params: params})
	},
	getEnvService(id){
		return http_request.get(`/env/env_service/${id}`)
	},
	updateEnvService(id, params){
		return http_request.put(`/env/env_service/${id}/`, params)
	},
	createEnvService(params){
		return http_request.post('/env/env_service/', params)
	},
	deleteEnvService(id){
		return http_request.delete(`/env/env_service/${id}/`)
	},
	// 平台管理接口
	getPlants(params){
		return http_request.get('/env/plant/', {params: params})
	},
	getPlant(id){
		return http_request.get(`/env/plant/${id}`)
	},
	updatePlant(id, params){
		return http_request.put(`/env/plant/${id}/`, params)
	},
	createPlant(params){
		return http_request.post('/env/plant/', params)
	},
	deletePlant(id){
		return http_request.delete(`/env/plant/${id}/`)
	},
	// 元素管理接口
	getElements(params){
		return http_request.get('/element/', {params: params})
	},
	getElement(id){
		return http_request.get(`/element/${id}`)
	},
	updateElement(id, params){
		return http_request.put(`/element/${id}/`, params)
	},
	createElement(params){
		return http_request.post('/element/', params)
	},
	deleteElement(id){
		return http_request.delete(`/element/${id}/`)
	},
	// 环境平台理接口
	getEnvPlants(params){
		return http_request.get('/env/env_plant/', {params: params})
	},
	getEnvPlant(id){
		return http_request.get(`/env/env_plant/${id}`)
	},
	updateEnvPlant(id, params){
		return http_request.put(`/env/env_plant/${id}/`, params)
	},
	createEnvPlant(params){
		return http_request.post('/env/env_plant/', params)
	},
	deleteEnvPlant(id){
		return http_request.delete(`/env/env_plant/${id}/`)
	},
	// 模块接口
	getModules(params){
		return http_request.get('/env/module/', {params: params})
	},
	getModule(id){
		return http_request.get(`/env/module/${id}`)
	},
	updateModule(id, params){
		return http_request.put(`/env/module/${id}/`, params)
	},
	createModule(params){
		return http_request.post('/env/module/', params)
	},
	deleteModule(id){
		return http_request.delete(`/env/module/${id}/`)
	},
	// 页面接口
	getAllPlantModule(params){
		return http_request.get('/env/allPlantModule/', {params: params})
	},
	getAllModulePage(params){
		return http_request.get('/env/allModulePage/', {params: params})
	},
	getAllPlantElement(params){
		return http_request.get('/env/allPlantElement/', {params: params})
	},
	getPages(params){
		return http_request.get('/env/page/', {params: params})
	},
	getPage(id){
		return http_request.get(`/env/page/${id}`)
	},
	updatePage(id, params){
		return http_request.put(`/env/page/${id}/`, params)
	},
	createPage(params){
		return http_request.post('/env/page/', params)
	},
	deletePage(id){
		return http_request.delete(`/env/page/${id}/`)
	},
	// 请求头
	getHeaders(params){
		return http_request.get('/env/header/', {params: params})
	},
	getHeader(id){
		return http_request.get(`/env/header/${id}`)
	},
	updateHeader(id, params){
		return http_request.put(`/env/header/${id}/`, params)
	},
	createHeader(params){
		return http_request.post('/env/header/', params)
	},
	deleteHeader(id){
		return http_request.delete(`/env/header/${id}/`)
	},
	// 脚本接口
	getPythons(params){
		return http_request.get('/script/python/', {params: params})
	},
	getPython(id){
		return http_request.get(`/script/python/${id}`)
	},
	updatePython(id, params){
		return http_request.put(`/script/python/${id}/`, params)
	},
	createPython(params){
		return http_request.post('/script/python/', params)
	},
	deletePython(id){
		return http_request.delete(`/script/python/${id}/`)
	},
	// API接口
	getApis(params){
		return http_request.get('/interface/api/', {params: params})
	},
	getApi(id){
		return http_request.get(`/interface/api/${id}`)
	},
	updateApi(id, params){
		return http_request.put(`/interface/api/${id}/`, params)
	},
	createApi(params){
		return http_request.post('/interface/api/', params)
	},
	deleteApi(id){
		return http_request.delete(`/interface/api/${id}/`)
	},
	// 步骤接口
	getActions(params){
		return http_request.get('/test/actions/', {params: params})
	},
	getSteps(params){
		return http_request.get('/test/step/', {params: params})
	},
	getStep(id){
		return http_request.get(`/test/step/${id}`)
	},
	updateStep(id, params){
		return http_request.put(`/test/step/${id}/`, params)
	},
	createStep(params){
		return http_request.post('/test/step/', params)
	},
	deleteStep(id){
		return http_request.delete(`/test/step/${id}/`)
	},
	// 用例接口
	getCheck(){
		return http_request.get('/test/check/')
	},
	getFuncParams(params){
		return http_request.get('/test/func_params/', {params: params})
	},
	caseRun(params){
		return http_request.post('/test/run/', params)
	},
	getCaseLogs(params){
		return http_request.get('/test/logs/', {params: params})
	},
	getCases(params){
		return http_request.get('/test/case/', {params: params})
	},
	getCase(id){
		return http_request.get(`/test/case/${id}`)
	},
	updateCase(id, params){
		return http_request.put(`/test/case/${id}/`, params)
	},
	createCase(params){
		return http_request.post('/test/case/', params)
	},
	deleteCase(id){
		return http_request.delete(`/test/case/${id}/`)
	},
	//标签
	getTags(params){
		return http_request.get('/test/tag/', {params: params})
	},
	getTag(id){
		return http_request.get(`/test/tag/${id}`)
	},
	updateTag(id, params){
		return http_request.put(`/test/tag/${id}/`, params)
	},
	createTag(params){
		return http_request.post('/test/tag/', params)
	},
	deleteTag(id){
		return http_request.delete(`/test/tag/${id}/`)
	},
	// 用户管理接口
	getUsers(params){
		return http_request.get('/user/users/', {params: params})
	},
	getUser(id){
		return http_request.get(`/user/users/${id}`)
	},
	updateUser(id, params){
		return http_request.put(`/user/users/${id}/`, params)
	},
	createUser(params){
		return http_request.post('/user/users/', params)
	},
	deleteUser(id){
		return http_request.delete(`/user/users/${id}/`)
	},
	// 权限管理接口
	getPermissions(params){
		return http_request.get('/user/permission', {params: params})
	},
	updatePermission(id, params){
		return http_request.put(`/user/permission/${id}/`, params)
	},
	createPermission(params){
		return http_request.post('/user/permission/', params)
	},
	deletePermission(id){
		return http_request.delete(`/user/permission/${id}/`)
	},
	// 角色管理接口
	getRole(id){
		return http_request.get(`/user/role/${id}/`)
	},
	getRoles(params){
		return http_request.get('/user/role/', {params: params})
	},
	updateRole(id, params){
		return http_request.put(`/user/role/${id}/`, params)
	},
	getRolePermission(id){
		return http_request.get(`/user/role_permission/${id}`)
	},
	createRolePermission(params){
		return http_request.post('/user/role_permission/', params)
	},
	updateRolePermission(id, params){
		return http_request.put(`/user/role_permission/${id}/`, params)
	},
	deleteRole(id){
		return http_request.delete(`/user/role/${id}/`)
	},
	// 登录接口
	loginApi(params){
		return http_request.post('/user/login/', params)
	},
	// 项目管理模块中的接口
	getProject(id){
		return http_request.get(`/projects/${id}/`)
	},
	getProjects(params){
		return http_request.get('/projects/', {params: params})
	},
	createProject(params){
		return http_request.post('/projects/', params)
	},
	deleteProject(id){
		return http_request.delete(`/projects/${id}/`)
	},
	updateProject(id, params){
		return http_request.put(`/projects/${id}/`, params)
	},
	// 项目管理模块中的接口
	getProjectAppeals(params){
		return http_request.get('/project_appeal/', {params: params})
	},
	updateProjectAppeal(id, params){
		return http_request.put(`/project_appeal/${id}/`, params)
	},
	createProjectAppeal(params){
		return http_request.post('/project_appeal/', params)
	},
	//接口管理模块中的接口
	getInterfaces(params){
		return http_request.get('interfaces/', {params: params})
	},
	createInterface(params){
		return http_request.post('/interfaces/', params)
	},
	updateInterface(id, params){
		return http_request.put(`/interfaces/${id}/`, params)
	},
	deleteInterface(id){
		return http_request.delete(`/interfaces/${id}/`)
	},
	//用例管理模块中的接口
	getTestCase(id){
		return http_request.get(`test_steps/${id}/`)
	},
	getTestCases(params){
		return http_request.get('test_steps/', {params: params})
	},
	createTestCase(params){
		return http_request.post('/test_steps/', params)
	},
	updateTestCase(id, params){
		return http_request.put(`/test_steps/${id}/`, params)
	},
	deleteTestCase(id){
		return http_request.delete(`/test_steps/${id}/`)
	},
	//套件管理模块中的接口
	getSuite(id){
		return http_request.get(`suite/${id}/`)
	},
	getSuites(params){
		return http_request.get('suite/', {params: params})
	},
	runSuite(params){
		return http_request.post('/suite/run/', params)
	},
	createSuite(params){
		return http_request.post('/suite/', params)
	},
	updateSuite(id, params){
		return http_request.put(`/suite/${id}/`, params)
	},
	deleteSuite(id){
		return http_request.delete(`/suite/${id}/`)
	},
	getTask(id){
		return http_request.get(`task/${id}/`)
	},
	getTasks(params){
		return http_request.get('task/', {params: params})
	},
	createTask(params){
		return http_request.post('/task/', params)
	},
	updateTask(id, params){
		return http_request.put(`/task/${id}/`, params)
	},
	deleteTask(id){
		return http_request.delete(`/task/${id}/`)
	},
	//报告管理模块中的接口
	getReport(id){
		return http_request.get(`report/${id}/`)
	},
	getReports(params){
		return http_request.get('report/', {params: params})
	},
	deleteReport(id){
		return http_request.delete(`/report/${id}/`)
	},
	
}